package com.nit.lab.controller.medical;

import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.medical.BedDTO;
import com.nit.lab.dto.utils.OpResultDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.service.medical.BedService;
import com.nit.lab.utils.HttpRequestReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/bed")
public class BedController {

    @Resource
    private BedService bedService;
    private final Logger logger = LoggerFactory.getLogger(BedController.class);

    /**
     * @Description: 获取数据列表
     * @Author HWY
     * @Date 2020/12/14
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = bedService.list4Table(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    /**
     * @Description: 新增记录
     * @Author HWY
     * @Date 2020/12/14
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestBody BedDTO bedDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(bedService.add(bedDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 更新记录
     * @Author HWY
     * @Date 2020/12/14
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody BedDTO unitDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(bedService.update(unitDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 删除记录
     * @Author HWY
     * @Date 2020/12/14
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String bedIds = jsonRequest.getString("bedIds");
            opResult.setMessage(bedService.remove(bedIds).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    @RequestMapping(value = "/getBedById", method = RequestMethod.POST)
    public OpResultDTO getBedById(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String bedId = jsonRequest.getString("bedId");
            opResult.setMessage("success");
            opResult.setResult(bedService.getBedById(Long.parseLong(bedId)));
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

}
